home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / PolyMazesDemo-c / B⁄W-Maze.c next >
Encoding:
C/C++ Source or Header  |  1994-12-04  |  5.3 KB  |  189 lines  |  [TEXT/MMCC]

  1. //•  ---------- "Source to 3D maze display" ---------- •//
  2.  
  3. //• Written Jul 16,  1985 by Steve Hawley (sdh@joevax.UUCP)
  4.  
  5. //• This is the source code for the maze display program. 
  6. //• It was done in Aztec C version 1.06D. 
  7.  
  8. //• Revived and made to run on Think C and Metrowerks C by
  9. //• Kenneth A. Long, at "itty bitty bytes(tm)".
  10.  
  11. WindowRecord    wRecord;
  12. WindowPtr    myWindow;
  13. EventRecord myEvent;
  14.  
  15. //• the maze. 1's = blocks,  0's = space.
  16. static char maze [22] [17] = { 
  17.     {1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1}, 
  18.     {1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1}, 
  19.     {1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1}, 
  20.     {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1}, 
  21.     {1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1}, 
  22.     {1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  23.     {1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1}, 
  24.     {1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1}, 
  25.     {1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1}, 
  26.     {1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1}, 
  27.     {1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1}, 
  28.     {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1}, 
  29.     {1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1}, 
  30.     {1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1}, 
  31.     {1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1}, 
  32.     {1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1}, 
  33.     {1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1}, 
  34.     {1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  35.     {1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1}, 
  36.     {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1}, 
  37.     {1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1}, 
  38.     {1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
  39. };        
  40.  
  41. //• Prototypes.
  42. int        Square_3_D(int col, int row);
  43. int        Draw_Maze(void);
  44. int        main(void);
  45.  
  46. //• Displays a given block from the maze. Decides on which side of 
  47. //• center the block is found,  and draws the visible faces,  which 
  48. //• are defined as polygons. The faces are projected using the
  49. //• formulae: 
  50. //•           'x' = x / z; 
  51. //•           'y' = y / z;         
  52. //• to achieve one point perspective.
  53.  
  54. Square_3_D (int col,  int row)
  55. {
  56.     int x1, x2, y1, y2, z1, z2;
  57.     PolyHandle left_side,  right_side,  top_side,  front_side;
  58.     
  59.     x1 = (col - 8) * 100;        //• 100 is width of side.
  60.     x2 = x1 + 100;
  61.     z1 = (23 - row);
  62.     z2 = z1 -1;
  63.     y1 = 150;
  64.     y2 = 250;
  65.     
  66.     left_side = OpenPoly ();
  67.     MoveTo ((x1 / z1) + 240,  (y1 / z1) + 60);
  68.     LineTo ((x1 / z1) + 240,  (y2 / z1) + 60);
  69.     LineTo ((x1 / z2) + 240,  (y2 / z2) + 60);
  70.     LineTo ((x1 / z2) + 240,  (y1 / z2) + 60);
  71.     LineTo ((x1 / z1) + 240,  (y1 / z1) + 60);
  72.     ClosePoly ();
  73.     
  74.     right_side = OpenPoly ();
  75.     MoveTo ((x2 / z1) + 240,  (y1 / z1) + 60);
  76.     LineTo ((x2 / z1) + 240,  (y2 / z1) + 60);
  77.     LineTo ((x2 / z2) + 240,  (y2 / z2) + 60);
  78.     LineTo ((x2 / z2) + 240,  (y1 / z2) + 60);
  79.     LineTo ((x2 / z1) + 240,  (y1 / z1) + 60);
  80.     ClosePoly ();
  81.     
  82.     top_side = OpenPoly ();
  83.     MoveTo ((x1 / z1) + 240,  (y1 / z1) + 60);
  84.     LineTo ((x2 / z1) + 240,  (y1 / z1) + 60);
  85.     LineTo ((x2 / z2) + 240,  (y1 / z2) + 60);
  86.     LineTo ((x1 / z2) + 240,  (y1 / z2) + 60);
  87.     LineTo ((x1 / z1) + 240,  (y1 / z1) + 60);
  88.     ClosePoly ();
  89.     
  90.     front_side = OpenPoly ();
  91.     MoveTo ((x1 / z2) + 240,  (y1 / z2) + 60);
  92.     LineTo ((x2 / z2) + 240,  (y1 / z2) + 60);
  93.     LineTo ((x2 / z2) + 240,  (y2 / z2) + 60);
  94.     LineTo ((x1 / z2) + 240,  (y2 / z2) + 60);
  95.     LineTo ((x1 / z2) + 240,  (y1 / z2) + 60);
  96.     ClosePoly ();
  97.     
  98.     //• Decide to draw right side (dark gray).
  99.     if (x2 < 0 && maze [row] [col + 1] != 1)
  100.     {
  101.         PenPat (&qd.dkGray);
  102.         ErasePoly (right_side);
  103.         PaintPoly (right_side);
  104.         PenPat (&qd.black);
  105.         FramePoly (right_side);
  106.     }
  107.     
  108.     //• Decide to draw left face (light gray).
  109.     if (x1 > 0 && maze [row] [col - 1] != 1) 
  110.     {
  111.         PenPat (&qd.ltGray);
  112.         ErasePoly (left_side);
  113.         PaintPoly (left_side);
  114.         PenPat (&qd.black);
  115.         FramePoly (left_side);
  116.     }
  117.  
  118.     //• Draw the top_side (white).
  119.     PenPat (&qd.white);
  120.     ErasePoly (top_side);
  121.     PaintPoly (top_side);
  122.     PenPat (&qd.black);
  123.     FramePoly (top_side);
  124.     
  125.     //• Draw the front_side (gray).
  126.     PenPat (&qd.gray);
  127.     ErasePoly (front_side);
  128.     PaintPoly (front_side);
  129.     PenPat (&qd.black);
  130.     FramePoly (front_side);
  131.     
  132.     //• Restore pen characteristics.
  133.     PenNormal (); 
  134.     
  135.     //• Dispose of all the polygons to free up memory.
  136.     KillPoly (top_side);        
  137.     KillPoly (front_side);
  138.     KillPoly (left_side);
  139.     KillPoly (right_side);
  140. }
  141.  
  142. //• Draws out all the blocks,  starting from the back,  moving left 
  143. //• to center,  then right to center.
  144.  
  145. Draw_Maze ()
  146. {
  147.     Rect trect;
  148.     int row,  col;
  149.     
  150.     for (row = 0; row < 22; row++)
  151.     {
  152.         for (col = 0; col < 8; col++)
  153.             if (maze [row] [col] == 1) 
  154.                 Square_3_D (col, row);
  155.         for (col = 16; col > 7; col--)
  156.             if (maze [row] [col] == 1) 
  157.                 Square_3_D (col,  row);
  158.     }
  159. }
  160.  
  161. main ()
  162. {
  163.     Rect screen;
  164.     
  165.     InitGraf (&qd.thePort);    /* do inits */
  166.     InitFonts ();
  167.     FlushEvents (everyEvent, 0);
  168.     InitWindows ();
  169.     SetCursor (&qd.arrow);
  170.     ShowCursor ();
  171.     
  172.     //• Set up the window.  Give it some bounds.
  173.     SetRect (&screen,  4,  40,  508,  370);
  174.     
  175.     //• Then create it from scratch.
  176.     myWindow = NewWindow (&wRecord,  &screen,  "\pSomething Amazing", 1, 0,  (WindowPtr)-1L, 1, 0L);
  177.  
  178.     SetPort (myWindow);                //• Say we'll draw there.
  179.     ShowWindow (myWindow);            //• Then show it.
  180.     
  181.     Draw_Maze ();                        //• Draw the maze in the window.
  182.     
  183.     //• Wait for button to be pressed or handle events 
  184.     //• like Clover-Shift-4, etc.
  185.     while (!Button ()) 
  186.         GetNextEvent (everyEvent,  &myEvent);
  187. }
  188. /* End of text from uiucdcs:net.sources.mac */
  189.